package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.SystemNewsInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/5.
 * 系统消息
 */

public class SystemNewsFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<SystemNewsInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new SystemNewsAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;

    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        SystemNewsInfo info = (SystemNewsInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
        UIHelper.showSendComplainDetial(getActivity(), args);

    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getSellerMsg(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),"seller",
                                    new JsonCallback<SystemNewsInfo>(SystemNewsInfo.class) {

                                        @Override
                                        public void onError(Call call, Exception e) {
                                            onRequestError();
                                        }

                                        @Override
                                        public void onResponse(
                                                ResponseInfo<SystemNewsInfo> response
                                        ) {
                                            int code = response.getCode();
                                            if (code == 1) {
                                                mErrorLayout.setErrorType(
                                                        EmptyLayout.NETWORK_ERROR);
                                                return;
                                            }
                                            infoList = response.getData();
                                            onRequestResponse(infoList);
                                        }

                                        @Override
                                        public void onAfter() {
                                            super.onAfter();
                                            onRequestFinish();
                                        }
                                    }
        );
    }

    public static class SystemNewsAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_system_news,
                                                                  parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(
                View view, int viewType
        ) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            SystemNewsInfo info = (SystemNewsInfo) _data.get(position);
            //vh.tv_orderId.setText("订单编号:"+info.getOrderid());
        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_news)
            TextView tv_news;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }

}
